<style>
    .layui-table-col-special .layui-table-cell {
        height: auto;
        overflow: visible;
        text-overflow: inherit;
        white-space: normal;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('dictionaries/add')?$auth->check('dictionaries/add'):'hide'}"
                        title="添加" data-type="add"><i
                        class="layui-icon layui-icon-add-1"></i>添加
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('dictionaries/edit')?$auth->check('dictionaries/edit'):'hide'}"
                        title="编辑" data-type="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('dictionaries/export')?$auth->check('dictionaries/export'):'hide'}"
                        title="导入" data-type="export"><i
                        class="iconfont icon-daoru5"></i>导入
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('dictionaries/del')?$auth->check('dictionaries/del'):'hide'}"
                        title="删除" data-type="batchdel"><i
                        class="layui-icon layui-icon-delete"></i>删除
                </button>
            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('dictionaries/index')}", create = "{:url('dictionaries/add')}",
        deleteUrl = "{:url('dictionaries/del')}", edit = "{:url('dictionaries/edit')}",uploadExcel = "{:url('dictionaries/export')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'soulTable', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table, soulTable = layui.soulTable, laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start_time' //指定元素
            , trigger: 'click'//呼出事件改成click
        });
        laydate.render({
            elem: '#end_time' //指定元素
            , trigger: 'click'//呼出事件改成click
        });
        table.render({
            elem: "#LAY-user-back-manage", url: listUrl
            , overflow: {
                type: 'tips'
                // ,hoverTime: 500 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                , color: 'white' // 字体颜色
                , bgColor: '#009688' // 背景色
            }
            , cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id", align: "center", title: "ID", hide: true},
                    {field: "title", align: "center", title: "字典名称", edit: 'text'},
                    {field: "dic_name", align: "center", title: "类别名称"},
                    {field: "table_name", align: "center", title: "来源表"}
                ]],
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            }, done: function (e) {
                soulTable.render(this);
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "count": res.total, //解析数据长度
                    "data": res.data, //解析数据列表
                };

            },
            text: {
                none: '呀！一条数据都没有'
            },

            page: true,
            limit: 20,
        });
        //监听单元格编辑
        table.on('edit(LAY-user-back-manage)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
            var param = $.param({'id': data.id, 'table': data.table_name, 'name': value});
            $.ajax({
                url: edit,
                type: 'post',
                data: param,
                beforeSend: function () {
                    layer.load(2, {
                        shade: [0.6, '#fff'], content: '数据处理中...', success: function (layero) {
                            layero.find('.layui-layer-content').css({
                                'padding-top': '6px',
                                'width': '150px',
                                'padding-left': '40px'
                            });
                            layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                'width': '150px !important',
                                'background-position': '2px 0 !important'
                            });
                        }
                    });
                },
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('LAY-user-back-manage');
                        layer.closeAll(); //关闭弹层
                    } else {

                        layer.msg(res.msg, {icon: 5});
                        return false;
                    }
                }
            });
        });
        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });
        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var new_arr = [];
                for (var i = 0; i < checkData.length; i++) {
                    new_arr.push({"id": checkData[i].id, 'table': checkData[i].table_name});
                }
                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: deleteUrl,
                        type: "post",
                        data: {
                            'object': new_arr
                        },
                        beforeSend: function () {
                            layer.load(2, {
                                shade: [0.6, '#fff'], content: '数据删除中...', success: function (layero) {
                                    layero.find('.layui-layer-content').css({
                                        'padding-top': '6px',
                                        'width': '150px',
                                        'padding-left': '40px'
                                    });
                                    layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                        'width': '150px !important',
                                        'background-position': '2px 0 !important'
                                    });
                                }
                            });
                        },
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.closeAll(); //关闭弹层
                            } else {
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '添加'
                    , content: create
                    , area: ['50%', '50%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            if (field.table_name == "") {
                                layer.msg("请选择数据字典类别");
                                return false;
                            }
                            if (field.name == "") {
                                layer.msg("请输入名称");
                                return false;
                            }
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: create,
                                type: 'post',
                                data: field,
                                beforeSend: function () {
                                    layer.load(2, {
                                        shade: [0.6, '#fff'], content: '数据新增中...', success: function (layero) {
                                            layero.find('.layui-layer-content').css({
                                                'padding-top': '6px',
                                                'width': '150px',
                                                'padding-left': '40px'
                                            });
                                            layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                                'width': '150px !important',
                                                'background-position': '2px 0 !important'
                                            });
                                        }
                                    });
                                },
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.closeAll(); //关闭弹层
                                    } else {

                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            },
            refresh: function () {
                table.reload('LAY-user-back-manage');
            },
            edit: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                if (checkData.length > 1) {
                    return layer.msg('只能选择一条信息');
                }
                var id = '';
                var table_name = '';
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                    table_name += checkData[i].table_name + ",";
                }
                id = id.substring(0, id.length - 1);
                table_name = table_name.substring(0, table_name.length - 1);
                var param = $.param({'id': id, 'table': table_name});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑'
                    , content: edit + '?' + param
                    , area: ['50%', '50%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            if (field.table_name == "") {
                                layer.msg("请选择数据字典类别");
                                return false;
                            }
                            if (field.name == "") {
                                layer.msg("请输入名称");
                                return false;
                            }
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: edit,
                                type: 'post',
                                data: field,
                                beforeSend: function () {
                                    layer.load(2, {
                                        shade: [0.6, '#fff'], content: '数据新增中...', success: function (layero) {
                                            layero.find('.layui-layer-content').css({
                                                'padding-top': '6px',
                                                'width': '150px',
                                                'padding-left': '40px'
                                            });
                                            layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                                'width': '150px !important',
                                                'background-position': '2px 0 !important'
                                            });
                                        }
                                    });
                                },
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.closeAll(); //关闭弹层
                                    } else {

                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            },
            export:function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '导入'
                    , content: uploadExcel
                    , area: ['50%', '50%']
                    , maxmin: true
                });
            }

        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>